<template>
  <div>
    <div class="gva-table-box">
      <el-table
        ref="multipleTable"
        :data="tableData"
        style="width: 100%"
        tooltip-effect="dark"
        row-key="ID"
        @row-click="handleRowClick"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column align="left" label="标题" prop="title" width="150" />
        <el-table-column align="left" label="需求" prop="require" width="200" />
        <!-- 需求方列 -->
        <el-table-column
          align="left"
          label="需求方"
          prop="publisher"
          width="150"
        />

        <!-- 候选技术方列 -->
        <el-table-column
          align="left"
          label="接单方"
          prop="candidates"
          width="200"
        />

        <!-- 价格列 -->
        <el-table-column align="left" label="价格" width="150">
          <template #default="scope">
            <div>{{ scope.row.ex_price }} 元</div>
          </template>
        </el-table-column>
        <el-table-column align="left" label="审核通过时间" width="150">
          <template #default="scope">
            <div>{{ new Date(scope.row.updated_at).toLocaleString() }}</div>
          </template>
        </el-table-column>
        <!-- 其他要求列 -->
        <el-table-column
          align="left"
          label="其他要求"
          prop="other"
          width="180"
        />
      </el-table>

      <div class="gva-pagination">
        <el-pagination
          :current-page="page"
          :page-size="pageSize"
          :page-sizes="[10, 30, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <el-dialog
      v-model="dialogFormVisible"
      width="50%"
      :before-close="closeDialog"
    >
      <template #title>
        <div>订单详情</div>
      </template>

      <!-- 子组件 OrderDetail 传递 orderId -->
      <OrderDetail v-if="dialogFormVisible" :orderId="currentOrderId" />

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { getMatchedOrderList } from "@/api/order";
import OrderDetail from "@/components/OrderDetails.vue";

import { useRouter } from "vue-router";
const router = useRouter();
const page = ref(1);
const total = ref(0);
const pageSize = ref(10);
const tableData = ref([]);
const candidateOptions = ref([]); // 候选人选项列表
const dialogFormVisible = ref(false);
const currentOrderId = ref(0);
// 处理点击行跳转
const handleRowClick = (row) => {
  currentOrderId.value = row.order_id;
  dialogFormVisible.value = true; // 跳转到 OrderDetails 页面，并传递 id
};

const closeDialog = () => {
  dialogFormVisible.value = false;
};

// 获取表格数据
const getTableData = async () => {
  tableData.value = [];
  const table = await getMatchedOrderList({
    page: page.value,
    pageSize: pageSize.value,
    type: 3,
  });
  if (table.code === 0) {
    table.data.list.forEach((item) => {
      item.candidateDeatils = item.candidates;
      let candidates = [];
      if (!item.candidates || item.candidates.length === 0) {
        item.candidates = "暂无候选人";
      } else {
        item.candidates.forEach((c) => {
          candidates.push(c.nickname + "(" + c.phone + ")");
        });
        item.candidates = candidates.join(",");
      }
    });
    tableData.value = table.data.list;
    total.value = table.data.total;
    page.value = table.data.page;
    pageSize.value = table.data.pageSize;
  }
};

getTableData();
</script>

<style scoped>
.gva-btn-list {
  margin-bottom: 20px;
}
.el-button {
  margin-left: 10px;
}
.flex {
  display: flex;
  justify-content: space-between;
}
.mt-4 {
  margin-top: 16px;
}
</style>
